package com.concurrent.frame.test;

import com.concurrent.frame.pojo.JobInfo;
import com.concurrent.frame.pojo.TaskResult;
import com.concurrent.frame.pojo.TaskResultType;
import com.concurrent.frame.processor.task.AbsTaskProConProcessor;

import java.util.Random;

public class MyTaskProCon extends AbsTaskProConProcessor<Integer,Integer,Integer> {
    public MyTaskProCon(JobInfo jobInfo) {
        super(jobInfo);
    }

    @Override
    public TaskResult<Integer> taskConsumerExecute(TaskResult<Integer> t) {
        try {

            //获取生产者的运算结果...(哪个线程先执行完,先获取谁的!)
            Integer value = t.getReturnValue();
            System.out.println("Consumer收到参数:"+value);
            //休眠...随机时长
            Thread.sleep(1000*(int)(Math.random()*10+1));
            //随机决定,执行成功与失败.
            boolean b = new Random().nextBoolean();
            //省略对获取参数,各种复杂性运算,原值返回!
            Integer result = value*12-11;
            System.out.println("Consumer任务完成！获取参数："+value+",处理结果:"+result);
            if (b){
                return  new TaskResult<Integer>(TaskResultType.Success,result);
            }else{
                return new TaskResult<Integer>(TaskResultType.Failure,result);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new TaskResult<Integer>(TaskResultType.Failure,-1);
    }


    @Override
    public TaskResult<Integer> taskProductExecute(Integer value) {
        int result ;
        try {
            Thread.sleep(1000*(int)(Math.random()*10+1));//休眠,随机时长
            boolean b = new Random().nextBoolean();//随机表示,任务是否执行成功.
            //....各种复杂性运算
            result  = value*100+100-123;
            System.out.println("Product任务完成！获取参数"+value +
                    "，处理结果："+result);

            if (b){
                return  new TaskResult<Integer>(TaskResultType.Success,result);
            }else{
                return new TaskResult<Integer>(TaskResultType.Failure,result);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new TaskResult<Integer>(TaskResultType.Failure,-1);
    }
}
